package com.leyou.search.listener;

import com.leyou.common.constants.MQConstants;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SearchListener {
    @Autowired
    private SearchService searchService;

    /**
     * 创建索引
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MQConstants.Exchange.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            value = @Queue(value = MQConstants.Queue.SEARCH_ITEM_UP,durable = "true"),
            key = MQConstants.RoutingKey.ITEM_UP_KEY
    ))
    public void addIndex(Long id) {
        searchService.addIndex(id);
    }

    /**
     * 删除索引
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MQConstants.Exchange.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            value = @Queue(value = MQConstants.Queue.SEARCH_ITEM_DOWN,durable = "true"),
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY
    ))
    public void delIndex(Long id) {
        searchService.delIndex(id);
    }
}
